﻿local L = LibStub( "AceLocale-3.0" ):GetLocale( "SimpleActionSets" )

local SAS_POS_TEMP;

function SimpleActionSets:Minimap_OnClick( self, button )
	-- Open up the main menu
	SAS_SetsDropDown:Hide();
	if ( button == "LeftButton" ) then
		if ( self:GetScript("OnUpdate") ) then
			SimpleActionSets:Minimap_DragStop(self)
		else
			SimpleActionSets:Main_Toggle();
		end
	else
		if ( self:GetScript("OnUpdate")) then
			SimpleActionSets:Minimap_DragStop(self);
			SimpleActionSets.db.profile.miniMapButton.position = SAS_POS_TEMP;
			SimpleActionSets:Minimap_PosUpdate();
		else
			SAS_SetsDropDown.point = "TOPRIGHT";
			SAS_SetsDropDown.relativePoint = "BOTTOMLEFT";
			ToggleDropDownMenu(1, nil, SAS_SetsDropDown, "SASMinimapButton", 0, 0);
		end
	end
end

function SimpleActionSets:Minimap_OnEnter( self )
	if ( SAS_SetsDropDown:IsVisible() ) then return; end
	GameTooltip:SetOwner(self, "ANCHOR_LEFT");
	GameTooltip:AddLine("Simple Action Sets");
	if ( SimpleActionSets.db.profile.miniMapButton.lock ) then
		GameTooltip:AddLine(L["SAS_MINIMAP_LOCKED_TOOLTIP"], 0.6, 0.6, 0.6);
	elseif ( self:GetScript("OnUpdate") ) then
		GameTooltip:AddLine(L["SAS_MINIMAP_DRAG_TOOLTIP"], 0.7, 0.7, 0.7);
	else
		GameTooltip:AddLine(L["SAS_MINIMAP_UNLOCKED_TOOLTIP"], 0.7, 0.7, 0.7);
	end
	GameTooltip:Show();
end

function SimpleActionSets:Minimap_OnLoad( self )
	self:RegisterForClicks("LeftButtonUp", "RightButtonUp");
	self:RegisterForDrag("LeftButton", "RightButton");
	tinsert(UISpecialFrames,"SASMain");
end

-- Dragging functions for the minimap button position
function SimpleActionSets:Minimap_DragStart( self )
	if ( not SimpleActionSets.db.profile.miniMapButton.lock ) then
		SAS_POS_TEMP = SimpleActionSets.db.profile.miniMapButton.position;
		self:SetScript("OnUpdate", SimpleActionSets.Minimap_DragUpdate); --SASMinimapFrame.BeingDragged = true;
	end
end

function SimpleActionSets:Minimap_DragStop( self )
	SASMinimapButton:UnlockHighlight()
	self:SetScript("OnUpdate", nil); --SASMinimapFrame.BeingDragged = nil;
end

function SimpleActionSets:Minimap_DragUpdate()
	-- Thanks to Gello for making this a ton shorter
	SASMinimapButton:LockHighlight();
	local curX, curY = GetCursorPosition();
	local mapX, mapY = Minimap:GetCenter();
	SimpleActionSets.db.profile.miniMapButton.position = deg(math.atan2( curY - mapY * Minimap:GetEffectiveScale(), mapX * Minimap:GetEffectiveScale() - curX ));
	SimpleActionSets:Minimap_PosUpdate();
end

function SimpleActionSets:Minimap_PosUpdate()
	SASMinimapFrame:SetPoint("TOPLEFT","Minimap","TOPLEFT",53-((80+self.db.profile.miniMapButton.offset)*cos(self.db.profile.miniMapButton.position or 0)),((80+self.db.profile.miniMapButton.offset)*sin(self.db.profile.miniMapButton.position or 0))-55);
	self:Options_Update();
end

function SimpleActionSets:Minimap_SetsDropDown_Initialize()
	-- Setup the minimap dropdown menu
	local info = {};
	local hasSets;

	info.text = L["SAS_TEXT_DROPDOWN_TITLE"];
	info.isTitle = 1;
	info.justifyH = "CENTER";
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);

	SimpleActionSets:SetsDropDown_Initialize();
end

function SimpleActionSets:Minimap_SetsDropDown_OnClick( self )
	-- minimap dropdown menu handler
	local id = self:GetID();
	if ( id == 2 ) then
		ShowUIPanel(SASMain);
	elseif ( id == 3 ) then
		SimpleActionSets:Actions_SaveNew();
	elseif ( id == 4 ) then
		SimpleActionSets:Warning("SAVE", function(set) SimpleActionSets:SaveSet(set) end, SimpleActionSets:GetCurrentSet());
	elseif ( id > 5 ) then
		SimpleActionSets:Debug( self:GetText() );
		SimpleActionSets:SwapSet( self:GetText() );
	end
end

function SimpleActionSets:Minimap_SetsDropDown_OnLoad( self )
	UIDropDownMenu_Initialize(self, SimpleActionSets.Minimap_SetsDropDown_Initialize, "MENU");
end

function SimpleActionSets:GetSetList()
	local list = {};
	if ( self.db and self.db.profile and self.db.profile.sets ) then
		for k, v in pairs(self.db.profile.sets) do
			tinsert(list,k);
		end
		sort(list);
	end

	if ( #list > 0 ) then
		return list;
	end
end

function SimpleActionSets:SetsDropDown_Initialize()

	local info = {};
	info.text = L["SAS_TEXT_DROPDOWN_OPEN"];
	info.notCheckable = 1;
	info.func = function(self) SimpleActionSets:Minimap_SetsDropDown_OnClick(self) end;
	UIDropDownMenu_AddButton(info);

	info = {};
	info.text = L["SAS_TEXT_DROPDOWN_SAVENEW"];
	info.notCheckable = 1;
	info.func = function(self) SimpleActionSets:Minimap_SetsDropDown_OnClick(self) end;
	UIDropDownMenu_AddButton(info);

	info = {};
	info.text = L["SAS_TEXT_DROPDOWN_SAVECURRENT"];
	info.notCheckable = 1;
	if ( not self:GetCurrentSet() ) then
		info.disabled = 1;
	end
	info.func = function(self) SimpleActionSets:Minimap_SetsDropDown_OnClick(self) end;
	UIDropDownMenu_AddButton(info);

	info = {};
	info.text = L["SAS_TEXT_DROPDOWN_SETS"];
	info.isTitle = 1;
	info.justifyH = "CENTER";
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);

	local list = self:GetSetList();
	if ( list ) then
		for k, v in ipairs(list) do
			info = { };
			info.text = v;
			info.isTitle = nil;
			if ( self:GetCurrentSet() == v ) then
				info.checked = 1;
			end
			info.func = function(self) SimpleActionSets:Minimap_SetsDropDown_OnClick(self) end;
			UIDropDownMenu_AddButton(info);
		end
	else
		info = { };
		info.text = L["SAS_TEXT_DROPDOWN_NONE"];
		info.disabled = 1;
		UIDropDownMenu_AddButton(info);
	end
end

function SimpleActionSets:GetMiniMapPosition()
	return self.db.profile.miniMapButton.position;
end

function SimpleActionSets:SetMiniMapPosition(value)
	self.db.profile.miniMapButton.position = value
end

function SimpleActionSets:GetMiniMapOffset()
	return self.db.profile.miniMapButton.offset;
end

function SimpleActionSets:SetMiniMapOffset(value)
	self.db.profile.miniMapButton.offset = value
end

function SimpleActionSets:GetMiniMapPositionTemp()
	return SAS_POS_TEMP;
end

function SimpleActionSets:SetMiniMapPositionTemp(value)
	SAS_POS_TEMP = value;
end